import os

import pandas
from openpyxl.reader.excel import load_workbook


def check():
    if not os.path.exists('template.xlsx'):
        print('缺少文件：template.xlsx')
    if not os.path.exists('error.xlsx'):
        print('缺少文件：error.xlsx')
    if not os.path.exists('config.ini'):
        print('缺少文件：config.ini')
    if os.path.exists('./input/input.xlsx'):
        os.remove('./input/input.xlsx')
    if not os.path.exists('input'):
        os.makedirs('input')
    if not os.path.exists('output'):
        os.makedirs('output')


def find_files():
    directory = "./input/"
    package = os.listdir(directory)
    if len(package) == 1:
        for filename in os.listdir(directory):
            if filename.endswith(".csv"):
                target = filename
                print('找到文件：' + filename)
        result = directory + target
        return result
    else:
        print("请确保input文件夹里有且只有一个需要转换的文件")
        return 'error'


def csv_to_xlsx_pd(source_path: str, save_path: str, encode='gbk'):
    csv = pandas.read_csv(source_path, encoding=encode)
    csv.to_excel(save_path, sheet_name='sheet')
    print('转换处理完毕')


def get_serial_number():
    wb = load_workbook('./input/input.xlsx')  # 打开已存在的工作簿
    ws = wb.active
    cell = ws["a6"]
    result = cell.value
    return result
